<HTML><BODY>
<?php

	if (!isset($_POST["started"]))
	{

?><FORM METHOD="POST">
<INPUT TYPE="hidden" NAME="started" VALUE="1">
<H1>PHPBTTracker Installation</H1>
The database needs to be prepared for the tracker. This script will help
you do that.<P>
You have two choices:<BR>
If you have a username, password and database for the tracker already
created, <INPUT TYPE="SUBMIT" NAME="preexisting" VALUE="Click Here">.<BR><BR>
If you need to create the account, and you have the username and password
of a user who can create user accounts and databases,
<INPUT TYPE="SUBMIT" NAME="makeaccount" VALUE="Click Here">.
<BR>
</BODY></HTML><?php exit;

	}
	if (isset($_POST["preexisting"]))
	{
		?>
<FORM METHOD="POST">
<INPUT TYPE="hidden" NAME="maketables" VALUE="1">
<INPUT TYPE="hidden" NAME="started" VALUE="1">
<H1>PHPBTTracker Installation</H1>
<BR><BR>
Database hostname (in MySQL format): <INPUT TYPE="text" NAME="host" VALUE="localhost" SIZE=40><BR>
Tracker's database username: <INPUT TYPE="text" NAME="username" SIZE="40"><BR>
Tracker's database password: <INPUT TYPE="password" NAME="password" SIZE=40><BR>
Database's name: <INPUT TYPE="text" NAME="database" SIZE=40><BR>
<INPUT TYPE="submit" VALUE="Finish">
<BR>
</FORM></BODY></HTML><?php exit;

	}
	if (isset($_POST["makeaccount"]))
	{
		?>
<FORM METHOD="POST">
<INPUT TYPE="hidden" NAME="domakeaccount" VALUE="1">
<INPUT TYPE="hidden" NAME="started" VALUE="1">
<H1>PHPBTTracker Installation</H1>
<BR><BR>
Username of database admin: <INPUT TYPE="text" NAME="adminname" SIZE=40><BR>
Password of database admin: <INPUT TYPE="password" NAME="adminpass" SIZE=40><BR>
Database's location: <INPUT TYPE="text" NAME="host" SIZE=40 VALUE="localhost"><BR>
<BR>
Create username: <INPUT TYPE="text" NAME="username" SIZE=40><BR>
Password: <INPUT TYPE="password" NAME="password" SIZE=40><BR>
Create database: <INPUT TYPE="text" NAME="database" SIZE=40><BR>
<BR><INPUT TYPE="submit" VALUE="Install">
</FORM></BODY></HTML>
	<?php
	exit;
	}
	$makenamemap= 'CREATE TABLE namemap (info_hash char(40) NOT NULL default "", filename varchar(250) NOT NULL default "", url varchar(250) NOT NULL default "", info varchar(250) NOT NULL default "", PRIMARY KEY(info_hash))'; 	
	$makesummary = 'CREATE TABLE summary (info_hash char(40) NOT NULL default "", dlbytes bigint unsigned NOT NULL default 0, seeds int unsigned NOT NULL default 0, leechers int unsigned NOT NULL default 0, finished int unsigned NOT NULL default 0, lastcycle int unsigned NOT NULL default "0", lastSpeedCycle int unsigned NOT NULL DEFAULT "0", speed bigint unsigned NOT NULL default 0, PRIMARY KEY (info_hash))';
	$maketimestamps = 'CREATE TABLE timestamps (info_hash char(40) not null, sequence int unsigned not null auto_increment, bytes bigint unsigned not null, delta smallint unsigned not null, primary key(sequence), key sorting (info_hash))';
	if (isset($_POST["maketables"]))
	{
		$username = $_POST["username"] or die("Script error.");
		$password = $_POST["password"]; // Blank passwords ?!?
		$database = $_POST["database"] or die("Script error.");
		$hostname = $_POST["host"] or die("Script error.");
	?>
<FORM METHOD="POST">
<INPUT TYPE="hidden" NAME="domakeaccount" VALUE="1">
<INPUT TYPE="hidden" NAME="started" VALUE="1">
<H1>PHPBTTracker Installation</H1>
<BR><BR>
<?php
	$db = mysql_connect($hostname, $username, $password) or die("Can't connect to database: ".mysql_error()); 
	mysql_select_db($database) or die("Can't select database: ".mysql_error());
	mysql_query($makesummary) or die("Can't make the summary table: ".mysql_error());
	mysql_query($makenamemap) or die("Can't make the namemap table: ".mysql_error());
	mysql_query($maketimestamps) or die("Can't make the timestamps table: ".mysql_error());
	echo 'Database was created successfully. Use <A HREF="newtorrents.php">newtorrents.php</A> to add torrents to the tracker.<BR><BR>Be sure to update <TT>config.php</TT> and delete this installation script.<BR><BR>';
	
	$fd = fopen("config.php", "w") or die("Warning: couldn't make config.php!");
	fwrite($fd, "<?php\n/* Tracker Configuration\n *\n *  This file provides configuration informatino for\n *  the tracker. The user-editable variables are at the top. It is\n *  recommended that you do not change the database settings\n *  unless you know what you are doing.\n */\n\n//Maximum reannounce interval.\n"
. '$GLOBALS["report_interval"] = 1800;'
. "\n\n//Minimum reannounce interval. Optional.\n"
. '$GLOBALS["min_interval"] = 300;'
. "\n\n//Number of peers to send in one request.\n"
. '$GLOBALS["maxpeers"] = 50;'."\n\n//If set to true, then the tracker will accept any and all\n//torrents given to it. Not recommended, but available if you need it.\n".'$GLOBALS["dynamic_torrents"] = false;'
. "\n\n//// If set to true, NAT checking will be performed.\n// This may cause trouble with some providers, so it's\n// off by default.\n".'$GLOBALS["NAT"] = false;'."\n\n"
."// Persistent connections: true or false.\n"
."// Check with your webmaster to see if you're allowed to use these.\n"
."// Highly recommended, especially for higher loads.\n"
.'$GLOBALS["persist"] = false;'."\n\n"
."// Allow users to override ip= ?\n// Enable this if you know people have a legit reason to use\n// this function. Leave disabled otherwise.\n"
.'$GLOBALS["ip_override"] = false;'."\n\n"
. "\n\n// Username and password for file uploads. YOU MUST SET THESE!\n"
. '$upload_username="username";'."\n".'$upload_password="password";'."\n\n"
. "// For heavily loaded trackers, set this to false. It will stop count the number\n"
. "// of downloaded bytes and the speed of the torrent, but will significantly reduce\n"
. "// the load.\n"
. '$GLOBALS["countbytes"] = true;'
. "\n\n// Table caches!\n"
. "// Lowers the load on all systems, but takes up more disk space.\n"
. "// You win some, you lose some. But since the load is the big problem,\n"
. "// grab this.\n"
. "//\n"
. "// Warning! Enable this BEFORE making torrents, or else run makecache.php\n"
. "// immediately, or else you'll be in deep trouble. The tables will lose\n"
. "// sync and the database will be in a somewhat \"stale\" state.\n"
. "\$GLOBALS[\"peercaching\"] = true;\n"
. "\n\n\n/////////// End of User Configuration ///////////\n\n\n\n"
. '$dbhost = "'
. $hostname .
 '";  $dbuser = "' . 
 $username . 
'";  $dbpass = "' .
 $password . 
 '";  $database = "' . 
$database . "\";\n\n?>");
	fclose($fd);
	echo '</BODY></HTML>';
	exit;
	}

	if (isset($_POST["domakeaccount"]))
	{
		$username = $_POST["username"] or die("Script error.");
		$password = $_POST["password"]; // Blank password !?!
		$database = $_POST["database"] or die("Script error.");
		$hostname = $_POST["host"] or die("Script error.");

		$dbadmin = $_POST["adminname"] or die("Script error");
		$dbpass = $_POST["adminpass"]; // !!!
		echo "<H1>PHPBTTracker Installation</H1><BR>\n";
		
		// Escaping strings will be ignored for now.
		$db = mysql_connect($hostname, $dbadmin, $dbpass) or die("Error connecting: ".mysql_error());
		mysql_select_db("mysql") or die("Can't select db \"mysql\":".mysql_error());

		mysql_query("INSERT INTO user SET user=\"$username\", password=PASSWORD(\"$password\"), host=\"\"") or die("Can't make user: ".mysql_error());
		mysql_query("INSERT INTO db SET Host=\"%\", db=\"$database\", user=\"$username\", select_priv='Y', Insert_priv='Y', Update_priv='Y', Delete_priv='Y', Create_priv='Y', Drop_priv='Y', Alter_priv='Y', index_priv='Y'") or die("Cannot insert into \"Db\": ".mysql_error());
		mysql_query("CREATE DATABASE $database") or die("Can't make database: ".mysql_error());
		
		mysql_query("FLUSH PRIVILEGES") or die("Can't flush privileges: ".mysql_error());
	
		mysql_select_db($database) or die("Can't select db \"$database\":".mysql_error());
	
		mysql_query($makesummary) or die("Can't make the summary table: ".mysql_error());
		mysql_query($makenamemap) or die("Can't make the namemap table: ".mysql_error());
		mysql_query($maketimestamps) or die("Can't make the timestamps table: ".mysql_error());
		echo 'Database was created successfully. Use <A HREF="newtorrents.php">newtorrents.php</A> to add torrents to the tracker.<BR><BR>Remember to look over <TT>config.php</TT> (automatically generated) and delete this installation script (<TT>install.php</TT>).<BR><BR>';

	$fd = fopen("config.php", "w") or die("Warning: couldn't make config.php!");
	fwrite($fd, "<?php\n/* Tracker Configuration\n *\n *  This file provides configuration informatino for\n *  the tracker. The user-editable variables are at the top. It is\n *  recommended that you do not change the database settings\n *  unless you know what you are doing.\n */\n\n//Maximum reannounce interval.\n"
. '$GLOBALS["report_interval"] = 1800;'
. "\n\n//Minimum reannounce interval. Optional.\n"
. '$GLOBALS["min_interval"] = 300;'
. "\n\n//Number of peers to send in one request.\n"
. '$GLOBALS["maxpeers"] = 50;'."\n\n//If set to true, then the tracker will accept any and all\n//torrents given to it. Not recommended, but available if you need it.\n".'$GLOBALS["dynamic_torrents"] = false;'
. "\n\n//// If set to true, NAT checking will be performed.\n// This may cause trouble with some providers, so it's\n// off by default.\n".'$GLOBALS["NAT"] = false;'."\n\n"
."// Persistent connections: true or false.\n"
."// Check with your webmaster to see if you're allowed to use these.\n"
."// Highly recommended, especially for higher loads.\n"
.'$GLOBALS["persist"] = false;'."\n\n"
."// Allow users to override ip= ?\n// Enable this if you know people have a legit reason to use\n// this function. Leave disabled otherwise.\n"
.'$GLOBALS["ip_override"] = false;'."\n\n"
. "\n\n// Username and password for file uploads. YOU MUST SET THESE!\n"
. '$upload_username="username";'."\n".'$upload_password="password";'."\n\n\n"
. "// For heavily loaded trackers, set this to false. It will stop count the number\n"
. "// of downloaded bytes and the speed of the torrent, but will significantly reduce\n"
. "// the load.\n"
. '$GLOBALS["countbytes"] = true;'
. "\n\n// Table caches!\n"
. "// Lowers the load on all systems, but takes up more disk space.\n"
. "// You win some, you lose some. But since the load is the big problem,\n"
. "// grab this.\n"
. "//\n"
. "// Warning! Enable this BEFORE making torrents, or else run makecache.php\n"
. "// immediately, or else you'll be in deep trouble. The tables will lose\n"
. "// sync and the database will be in a somewhat \"stale\" state.\n"
. "\$GLOBALS[\"peercaching\"] = true;\n"
. "\n\n\n/////////// End of User Configuration ///////////\n\n\n\n"
. '$dbhost = "'
. $hostname .
 '";  $dbuser = "' . 
 $username . 
'";  $dbpass = "' .
 $password . 
 '";  $database = "' . 
$database . "\";\n\n?>");
	fclose($fd);
	echo '</BODY></HTML>';
	exit;

	}
	echo "Script error: no commands to execute.";



?>